package com.gqzm.iaas.service.impl;

import com.google.common.collect.Lists;
import com.gqzm.iaas.component.TaskCache;
import com.gqzm.iaas.component.TaskInfo;
import com.gqzm.iaas.domain.bo.BizException;
import com.gqzm.iaas.domain.dto.StartAnnotationTaskDTO;
import com.gqzm.iaas.domain.dto.StopTaskDTO;
import com.gqzm.iaas.domain.dto.TaskQueryDTO;
import com.gqzm.iaas.domain.dto.TaskStatusVO;
import com.gqzm.iaas.enums.ErrorCode;
import com.gqzm.iaas.service.IaasTaskService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName: {@link IaasTaskServiceImpl}
 * @description: TODO
 * @author: peter qin
 * @date: 2023 11 22
 * @version: 1.0
 **/
@Slf4j
@Service
public class IaasTaskServiceImpl implements IaasTaskService {


    @Override
    @SneakyThrows
    public String createSmartAnnotation(StartAnnotationTaskDTO dto) {
        Path path = Paths.get(dto.getFilePath());
        File f = path.toFile();
        if (!f.exists() || !f.isDirectory()) {
            throw BizException.of(
                    ErrorCode.BASE_FAILED,
                    "文件参数对应目录不存在或目录不是文件夹!"
            );
        }
        File[] fs = f.listFiles();
        if (fs == null || fs.length <= 0) {
            throw BizException.of(
                    ErrorCode.BASE_FAILED,
                    "没有可用的标注图片!"
            );
        }
        TaskCache.enqueue(TaskInfo.builder()
                .taskId(dto.getTaskId())
                .currentIndex(0)
                .picPath(dto.getFilePath())
                .resultPath(dto.getResultPath())
                .status("2")
                .picNames(Arrays.stream(fs)
                        .map(File::getName)
                        .filter(fn -> (fn.endsWith(".jpg")
                                || fn.endsWith(".jpeg")
                                || fn.endsWith(".png")
                                || fn.endsWith(".bmp"))
                                && !dto.getExcludeFileNames().contains(fn))
                        .collect(Collectors.toList()))
                .build());
        return dto.getFilePath();
    }


    @Override
    public List<TaskStatusVO> queryTasks(TaskQueryDTO dto) {
        return dto.getTaskIds().stream()
                .map(id -> {
                    TaskInfo taskInfo = TaskCache.getTaskInfo(id);
                    if (Objects.isNull(taskInfo)) {
                        return null;
                    }
                    return TaskStatusVO.builder()
                            .taskId(taskInfo.getTaskId())
                            .message(taskInfo.getMessage())
                            .status(taskInfo.getStatus())
                            .build();
                })
                .collect(Collectors.toList());
    }


    @Override
    public void stopTask(StopTaskDTO dto) {
        TaskCache.stopTask(dto.getTaskId());
    }
}
